package com.lagou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lagou.dao.UserMapper;
import com.lagou.domain.*;
import com.lagou.service.UserService;
import com.lagou.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {

        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        List<User> users = userMapper.findAllUserByPage(userVo);

        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    @Override
    public User login(User user) throws Exception {

        User userLogin = userMapper.login(user);

        if (null != userLogin && Md5.verify(user.getPassword(), "lagou", userLogin.getPassword())) {
            return userLogin;
        }

        return null;
    }

    @Override
    public List<Role> findUserRelationRoleById(Integer id) {
        return userMapper.findUserRelationRoleById(id);
    }

    @Override
    public void userContextRole(UserVo userVo) {

        userMapper.deleteUserContextRole(userVo.getUserId());

        Date date = new Date();
        for (Integer roleId : userVo.getRoleIdList()) {

            User_Role_relation userRoleRelation = new User_Role_relation();

            userRoleRelation.setUserId(userVo.getUserId());
            userRoleRelation.setRoleId(roleId);
            userRoleRelation.setCreatedTime(date);
            userRoleRelation.setUpdatedTime(date);
            userRoleRelation.setCreatedBy("system");
            userRoleRelation.setUpdatedby("system");

            userMapper.userContextRole(userRoleRelation);
        }
    }

    @Override
    public void updateUserStatus(User user) {

        user.setUpdate_time(new Date());
        userMapper.updateUserStatus(user);
    }

    /**
     * 获取用户权限, 动态展示
     * @param userId
     * @return
     */
    @Override
    public ResponseResult getUserPermissions(Integer userId) {

        //1. 获取用户当前的角色
        List<Role> roleList = userMapper.findUserRelationRoleById(userId);

        ArrayList<Integer> rolesIds = new ArrayList<>();

        for (Role role : roleList) {
            rolesIds.add(role.getId());
        }

        List<Menu> parentMenuByRoleId = userMapper.findParentMenuByRoleId(rolesIds);

        for (Menu menu : parentMenuByRoleId) {
            List<Menu> subMenuByPid = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenuByPid);
        }

        List<Resource> resourceList = userMapper.findResourceByRoleId(rolesIds);

        HashMap<String, Object> map = new HashMap<>();
        map.put("menuList", parentMenuByRoleId);
        map.put("resourceList", resourceList);

        return new ResponseResult(true, 200, "获取用户权限信息成功", map);
    }
}
